﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cosmos.Assembler.x86;
using UI = DimOS.DimSH.Interface;
using DimOS.Constants;
using DimOS.DimFS;
using DimOS.Internals;
using DimOS.VTS;
using DimOS.H;
using CORE = Cosmos.Core;
using SH = DimOS.DimSH.Shell;
using TERM = DimOS.DimSH.Terminal;

// ReSharper disable InconsistentNaming

namespace DimOS.DimSH
{
    public static class Initialize
    {
        private static bool _advancedTerminalEnabled;

        public static void Self()
        {
            var IS = new CORE.IOPort(0x3DA); //Input Status #1 register
            var AC = new CORE.IOPort(0x3C0); //Attribute Control Write register

            //Reading from IS triggers AC to await address/index pointing to specific register
            var temp = IS.Byte; AC.Byte = 0x10 | 0x20; //Point AC to 10h, don't clear the screen
            var value = new CORE.IOPort(0x3C1).Byte; //Read value from Attribute Control Read register
            AC.Byte = (byte)(value & 0xF7); //Write the value to AC with 4th bit (blinking) set to 0

            _advancedTerminalEnabled = Settings.AdvancedTerminal;
            TERM.SetCursorSize(20); Settings.AdvancedTerminal = false;
            TERM.HideCursor();

            for (int i = 0; i < UI.InfoColumns; i++)
            {
                UI.InfoSpaceSplitter += Separators.SpaceSplitter;
                UI.InfoSectionSplitter += Separators.InfoSplitter;
            }

            for (int i = 0; i < UI.OutputColumns; i++)
            {
                UI.OutputSpaceSplitter += Separators.SpaceSplitter;
                UI.OutputInitSplitter += Separators.InitSplitter;
                UI.OutputSectionSplitter += Separators.SectionSplitter;
                UI.JournalSplitter += Separators.InfoSplitter;
            }
        }

        public static void Logo() { UI.DrawLogo(); UI.DrawSectionSplitter(true, true); }

        public static void Hardware() { UI.DrawInitIndicator(Messages.InitHardware, 4,
                                        CORE.PCI.Setup, Colors.InitHardware);
                                        UI.DrawSectionSplitter(true, true); }

        public static void FileSystem() { UI.DrawInitIndicator(Messages.InitFileSystem, 4,
                                          Layout.Initialize, Colors.InitFileSystem);
                                          UI.DrawSectionSplitter(true, true); }

        public static void CommandShell() { UI.DrawInitIndicator(Messages.InitCommandShell, 4,
                                            SH.Initialize, Colors.InitCommandShell); Engine
                                            .Initialize(); UI.DrawSectionSplitter(true, true); }

        public static void Finish() { UI.DrawGreeting(); TERM.WriteLine(false); UI.Initialize();
                                      Settings.AdvancedTerminal = _advancedTerminalEnabled; VTS
                                      .Console.CurrentScreen = Screens.List[0]; for (int i = 0;
                                      i < UI.Rows - 2; i++) for (int j = 0; j < UI.OutputColumns;
                                      j++) { VTS.Console.CurrentScreen[j, i] = new CharColor(Screen
                                      .GetCharacter(j, i), Screen.GetColor(j, i)); TERM.ShowCursor();} }
    }
}
